package com.dmap.analysispoint.service.impl;

import com.dmap.analysispoint.bo.DateAndNumBo;
import com.dmap.analysispoint.controller.background.param.CountParam;
import com.dmap.analysispoint.dao.BurialPointAddUserMapper;
import com.dmap.analysispoint.dao.BurialPointDayUserMapper;
import com.dmap.analysispoint.dao.BurialPointHourUserMapper;
import com.dmap.analysispoint.po.BurialPointAddUserExample;
import com.dmap.analysispoint.po.BurialPointDayUserExample;
import com.dmap.analysispoint.po.BurialPointHourUserExample;
import com.dmap.analysispoint.service.UserInfoAnalysisService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * \* @author:zhouhao
 * \* Date:11/05/2018
 * \* Description:
 * \*
 */
@Service
public class UserInfoAnalysisServiceImpl  implements UserInfoAnalysisService {

    @Resource
    private BurialPointAddUserMapper burialPointAddUserMapper;

    @Resource
    private BurialPointDayUserMapper burialPointDayUserMapper;

    @Resource
    private BurialPointHourUserMapper burialPointHourUserMapper;

    /**
     *统计新增用户总数
     */
    @Override
    public Integer countAddUserNum(CountParam param) {
        if (param == null) {
            return 0;
        }
        BurialPointAddUserExample example = this.getParamExample(param);
        return burialPointAddUserMapper.countUserByExample(example);
    }

    /**
     *按小时分组统计新增用户总数
     */
    @Override
    public List<DateAndNumBo> countAddUserList(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointAddUserExample example = this.getParamExample(param);
        return burialPointAddUserMapper.countUserListByExample(example);
    }

    /**
     *BurialPointAddUser表查询时对查询条件进行处理
     */
    private BurialPointAddUserExample getParamExample(CountParam param) {
        BurialPointAddUserExample example = new BurialPointAddUserExample();
        BurialPointAddUserExample.Criteria criteria = example.createCriteria();
        if (param.getCompanyId() != null){
            criteria.andCompanyIdEqualTo(param.getCompanyId());
        }
        if (!StringUtils.isBlank(param.getProjectId())){
            criteria.andProjectIdEqualTo(param.getProjectId());
        }
        if (param.getPlaceId() != null){
            criteria.andPlaceIdEqualTo(param.getPlaceId());
        }
        if (!StringUtils.isBlank(param.getAppVersion())) {
            criteria.andAppVersionEqualTo(param.getAppVersion());
        }
        if (!StringUtils.isBlank(param.getFirmwareId())) {
            criteria.andFirmwareIdEqualTo(param.getFirmwareId());
        }
        if (!StringUtils.isBlank(param.getFirmwareVersion())) {
            criteria.andFirmwareVersionEqualTo(param.getFirmwareVersion());
        }
        if (param.getStartTime() != null && param.getEndTime() != null && param.getEndTime() != 0) {
            criteria.andCreateTimeBetween(param.getStartTime(), param.getEndTime());
        }
        return example;
    }

    /**
     *统计活跃用户总数（跨小时）
     */
    @Override
    public Integer countActiveUserNumByHour(CountParam param) {
        if (param == null) {
            return 0;
        }
        BurialPointHourUserExample example = this.getHourParamExample(param);
        return burialPointHourUserMapper.countUserByExample(example);
    }

    /**
     *按小时分组统计活跃用户总数
     */
    @Override
    public List<DateAndNumBo> countActiveUserListByHour(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointHourUserExample example = this.getHourParamExample(param);
        return burialPointHourUserMapper.countActiveUserListByExample(example);
    }

    /**
     *统计启动次数（跨小时）
     */
    @Override
    public Integer countStartUpNumByHour(CountParam param) {
        if (param == null) {
            return 0;
        }
        BurialPointHourUserExample example = this.getHourParamExample(param);
        return burialPointHourUserMapper.sumStartUpNum(example);
    }

    /**
     *按小时分组统计启动次数
     */
    @Override
    public List<DateAndNumBo> countStartUpNumList(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointHourUserExample example = this.getHourParamExample(param);
        return burialPointHourUserMapper.countStartUpNumListByExample(example);
    }

    /**
     *统计使用时长（跨天）
     */
    @Override
    public Integer countUseTimeByDay(CountParam param) {
        if (param == null) {
            return 0;
        }
        BurialPointDayUserExample example = this.getDayParamExample(param);
        return burialPointDayUserMapper.sumUseTimeByExample(example);
    }


    /**
     *BurialPointHourUser表查询时对查询条件进行处理
     */
    private BurialPointHourUserExample getHourParamExample(CountParam param) {
        BurialPointHourUserExample example = new BurialPointHourUserExample();
        BurialPointHourUserExample.Criteria criteria = example.createCriteria();
        if (param.getCompanyId() != null){
            criteria.andCompanyIdEqualTo(param.getCompanyId());
        }
        if (!StringUtils.isBlank(param.getProjectId())){
            criteria.andProjectIdEqualTo(param.getProjectId());
        }
        if (param.getPlaceId() != null){
            criteria.andPlaceIdEqualTo(param.getPlaceId());
        }
        if (!StringUtils.isBlank(param.getAppVersion())) {
            criteria.andAppVersionEqualTo(param.getAppVersion());
        }
        if (!StringUtils.isBlank(param.getFirmwareId())) {
            criteria.andFirmwareIdEqualTo(param.getFirmwareId());
        }
        if (!StringUtils.isBlank(param.getFirmwareVersion())) {
            criteria.andFirmwareVersionEqualTo(param.getFirmwareVersion());
        }
        if (param.getStartTime() != null && param.getEndTime() != null && param.getEndTime() != 0) {
            criteria.andLoginTimeBetween(param.getStartTime(), param.getEndTime());
        }
        return example;
    }

    /**
     *统计活跃用户数（跨天）
     */
    @Override
    public Integer countActiveUserNumByDay(CountParam param) {
        if (param == null) {
            return 0;
        }
        BurialPointDayUserExample example = this.getDayParamExample(param);
        return burialPointDayUserMapper.countActiveUserNumByExample(example);
    }

    /**
     *统计启动次数（跨天）
     */
    @Override
    public Integer countStartUpNumByDay(CountParam param) {
        if (param == null) {
            return 0;
        }
        BurialPointDayUserExample example = this.getDayParamExample(param);
        return burialPointDayUserMapper.sumStartUpNumByDay(example);
    }

    /**
     *BurialPointDayUser表查询时对查询条件进行处理
     */
    private BurialPointDayUserExample getDayParamExample(CountParam param) {
        BurialPointDayUserExample example = new BurialPointDayUserExample();
        BurialPointDayUserExample.Criteria criteria = example.createCriteria();
        if (param.getCompanyId() != null){
            criteria.andCompanyIdEqualTo(param.getCompanyId());
        }
        if (!StringUtils.isBlank(param.getProjectId())){
            criteria.andProjectIdEqualTo(param.getProjectId());
        }
        if (param.getPlaceId() != null){
            criteria.andPlaceIdEqualTo(param.getPlaceId());
        }
        if (!StringUtils.isBlank(param.getAppVersion())) {
            criteria.andAppVersionEqualTo(param.getAppVersion());
        }
        if (!StringUtils.isBlank(param.getFirmwareId())) {
            criteria.andFirmwareIdEqualTo(param.getFirmwareId());
        }
        if (!StringUtils.isBlank(param.getFirmwareVersion())) {
            criteria.andFirmwareVersionEqualTo(param.getFirmwareVersion());
        }
        if (param.getStartTime() != null && param.getEndTime() != null && param.getEndTime() != 0) {
            criteria.andLoginTimeBetween(param.getStartTime(), param.getEndTime());
        }
        return example;
    }

}
